# 导入必要的库
import re
import urllib.request
from urllib.error import URLError, HTTPError
import requests


# 定义要访问的URL
urls = [
    "https://bj.fang.com/quanwangso/search.html?city=bj&refer=sy_seach",
    "https://www.guazi.com/bj/buy/"
]
# 处理文件名中的非法字符
def sanitize_filename(filename):
    return re.sub(r'[\\/*?:"<>|]', "", filename)
# 使用urllib库访问URL
def access_with_urllib(url):
    try:
        # 发送请求
        response = urllib.request.urlopen(url)
        
        # 获取状态码
        status_code = response.getcode()
        
        # 获取返回对象的属性
        headers = response.headers
        
        # 获取返回体
        content = response.read().decode('utf-8')
        
        # 打印结果
        print(f"URL: {url} (urllib)")
        print(f"Status Code: {status_code}")
        print(f"Headers: {headers}")
        print(f"Content (first 100 characters): {content[:100]}")

         # 生成合法的文件名
        filename = sanitize_filename(f"{url.split('/')[-1]}_urllib.html")
        
        # 保存结果到文件
        with open(filename, 'w', encoding='utf-8') as file:
            file.write(content)
    
    except HTTPError as e:
        print(f"HTTP Error: {e.code} - {e.reason}")
    except URLError as e:
        print(f"URL Error: {e.reason}")

# 使用requests库访问URL
def access_with_requests(url):
    try:
        # 发送请求
        response = requests.get(url)
        
        # 获取状态码
        status_code = response.status_code
        
        # 获取返回对象的属性
        headers = response.headers
        
        # 获取返回体
        content = response.text
        
        # 打印结果
        print(f"URL: {url} (requests)")
        print(f"Status Code: {status_code}")
        print(f"Headers: {headers}")
        print(f"Content (first 100 characters): {content[:100]}")

        # 生成合法的文件名
        filename = sanitize_filename(f"{url.split('/')[-1]}_requests.html")
        
        # 保存结果到文件
        with open(filename, 'w', encoding='utf-8') as file:
            file.write(content)
    
    except requests.exceptions.RequestException as e:
        print(f"Request Error: {e}")

# 主函数
def main():
    for url in urls:
        access_with_urllib(url)
        access_with_requests(url)

if __name__ == "__main__":
    main()